using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

public class MailboxService : Service
{
    private int mailboxId;
    private string password;

    public int MailboxId
    {
        get { return mailboxId; }
        set { mailboxId = value; }
    }

    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public MailboxService() { }

    protected MailboxService(
        int mailboxId,
        int serviceId,
        int accountId,
        string title,
        string password,
        DateTime createDate,
        DateTime updateDate)
        : base(serviceId, accountId, title, createDate, updateDate)
    {
        this.password = password;
    }

    internal static MailboxService Create(string title, string password, int accountId)
    {
        int mailboxId, serviceId;
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("MailboxCreate", connection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("title", title);
            command.Parameters.AddWithValue("password", password);
            command.Parameters.AddWithValue("accountId", accountId);

            command.Parameters.Add("mailboxId", SqlDbType.Int).Direction = ParameterDirection.Output;
            command.Parameters.Add("serviceId", SqlDbType.Int).Direction = ParameterDirection.Output;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            mailboxId = (int)command.Parameters["mailboxId"].Value;
            serviceId = (int)command.Parameters["serviceId"].Value;
        }

        return new MailboxService(
            mailboxId, serviceId, accountId, title, password, DateTime.Now, DateTime.Now);
    }

    internal static List<MailboxService> Fetch()
    {
        List<MailboxService> mailboxList = new List<MailboxService>();
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("MailboxFetch", connection);
            command.CommandType = CommandType.StoredProcedure;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                MailboxService mailbox = new MailboxService(
                    (int)reader["MailboxId"],
                    (int)reader["ServiceId"],
                    (int)reader["AccountId"],
                    (string)reader["Title"],
                    (string)reader["Password"],
                    (DateTime)reader["CreateDate"],
                    (DateTime)reader["UpdateDate"]);

                mailboxList.Add(mailbox);
            }
            connection.Close();
        }
        return mailboxList;
    }
}